package com.jeecms.wicool.manager.orders.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.orders.OrdersDao;
import com.jeecms.wicool.dao.shops.ScoreBuySetDao;
import com.jeecms.wicool.dao.shops.ScoreShareSetDao;
import com.jeecms.wicool.dao.user.ScoreTradeDao;
import com.jeecms.wicool.dao.user.UserAccountDao;
import com.jeecms.wicool.entity.orders.Orders;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.entity.user.ScoreTrade;
import com.jeecms.wicool.entity.user.UserAccount;
import com.jeecms.wicool.entity.user.UserInfo;
import com.jeecms.wicool.manager.orders.OrderProductMappingMng;
import com.jeecms.wicool.manager.orders.OrdersMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import com.jeecms.wicool.manager.user.UserAccountMng;
import com.jeecms.wicool.manager.user.UserInfoMng;

@Service
@Transactional
public class OrdersMngImpl implements OrdersMng {

	private OrdersDao dao;

	@Autowired
	public void setDao(OrdersDao dao) {
		this.dao = dao;
	}

	@Transactional(readOnly = true)
	public Pagination getPage(String mobile, Integer disAddId,
			String orderCode, Integer shopId, String userName, Short status,
			Integer type, String startTime, String endTime, int pageNo,
			int pageSize, CmsUser user) {
		Pagination page = dao.getPage(mobile, disAddId, orderCode, shopId,
				userName, status, type, startTime, endTime, pageNo, pageSize,
				user);
		return page;
	}

	@Override
	public Orders findById(Integer id) {
		Orders entity = dao.findById(id);
		return entity;
	}

	@Override
	public Orders update(Orders bean, Integer shopId, Integer userId) {
		UserInfo user = userMng.findById(userId);
		ShopInfo shop = shopMng.findById(shopId);
		bean.setUser(user);
		bean.setShop(shop);
		Updater<Orders> updater = new Updater<Orders>(bean);
		bean = dao.updateByUpdater(updater);

		if (bean.getStatus().shortValue() == 3 && bean.getTotalPrice() != null) {
			// 加积分
			List<UserAccount> accountList = userAccountDao
					.getListUserAccountByUserId(user.getId());
			if (accountList != null && !accountList.isEmpty()) {
				UserAccount account = accountList.get(0);
				Integer score1 = bean.getTotalPrice().intValue();
				Integer score2 = account.getScore() == null ? 0 : account
						.getScore();
				account.setScore(score2 + score1);
				userAccountMng.update(account, shopId, user.getId());
				// 增加积分发放记录
				ScoreTrade trade = new ScoreTrade();
				trade.setScoreNum(score1);
				trade.setTradeSource(2);
				trade.setTradeTime(new Timestamp(System.currentTimeMillis()));
				trade.setTradeType(1);
				trade.setUser(user);
				scoreTradeDao.save(trade);
			}
		}

		// 确认收货之后分配积分给转接人和自己
		/*
		 * if(bean.getStatus().shortValue() == 4){ UserInfo sceneUser =
		 * user.getSceneUser(); // 给转接人加积分 if(user.getIsScoreFinish() == 0 &&
		 * sceneUser != null){ List<ScoreShareSet> list =
		 * scoreShareSetDao.getAll(new CmsUser()); if(list != null &&
		 * !list.isEmpty()){ ScoreShareSet scoreSet = list.get(0);
		 * List<UserAccount> accountList =
		 * userAccountDao.getListUserAccountByUserId(sceneUser.getId());
		 * if(accountList != null && !accountList.isEmpty()){ // 加积分 UserAccount
		 * account = accountList.get(0); Integer score1 =
		 * scoreSet.getScoreValue()== null ? 0 : scoreSet.getScoreValue();
		 * Integer score2 = account.getScore() == null? 0 : account.getScore();
		 * account.setScore(score2+score1);
		 * userAccountMng.update(account,shopId,sceneUser.getId()); // 增加积分发放记录
		 * ScoreTrade trade = new ScoreTrade(); trade.setScoreNum(score1);
		 * trade.setTradeSource(2); trade.setTradeTime(new
		 * Timestamp(System.currentTimeMillis())); trade.setTradeType(1);
		 * trade.setUser(sceneUser); scoreTradeDao.save(trade); // 已发放积分给转接人
		 * user.setIsScoreFinish(1); userMng.update(user, shopId,
		 * sceneUser.getId()); } } } // 给当事人发放积分 ScoreBuySet scoreSet =
		 * scoreBuySetDao.getByTradeMoney(bean.getOrderPrice().intValue());
		 * if(scoreSet != null){ // 加积分 List<UserAccount> accountList =
		 * userAccountDao.getListUserAccountByUserId(user.getId());
		 * if(accountList != null && !accountList.isEmpty()){ UserAccount
		 * account = accountList.get(0); Integer score1 =
		 * scoreSet.getScoreValue()== null ? 0 : scoreSet.getScoreValue();
		 * Integer score2 = account.getScore() == null? 0 : account.getScore();
		 * account.setScore(score2+score1);
		 * userAccountMng.update(account,shopId,user.getId()); // 增加积分发放记录
		 * ScoreTrade trade = new ScoreTrade(); trade.setScoreNum(score1);
		 * trade.setTradeSource(2); trade.setTradeTime(new
		 * Timestamp(System.currentTimeMillis())); trade.setTradeType(1);
		 * trade.setUser(user); scoreTradeDao.save(trade); } }
		 * 
		 * }
		 */

		return bean;
	}

	@Override
	public List<Orders> getAllGroupBuyOrders(Integer groupBuyId) {
		return dao.getAllGroupBuyOrders(groupBuyId);
	}

	@Override
	public List<Orders> getAll(CmsUser user) {
		return dao.getAll(user);
	}

	@Override
	public Pagination getPage(Integer type, Integer activityId, int pageNo,
			int pageSize) {
		return dao.getPage(type, activityId, pageNo, pageSize);
	}

	@Override
	public List<Orders> list(Short[] status, Integer disAddId, Date startTime,
			Date endTime, Integer typeId) {
		return dao.list(status, disAddId, startTime, endTime, typeId);
	}

	@Override
	public Pagination list(Short[] status, Integer disAddId, Date startTime,
			Date endTime, Integer typeId, Integer pageNo, Integer pageSize) {
		return dao.list(status, disAddId, startTime, endTime, typeId, pageNo,
				pageSize);
	}

	public List<Orders> list(String startTime, String endTime, Integer typeId) {
		return dao.list(startTime, endTime, typeId);
	}

	public List<Orders> list(String startTime, String endTime) {
		return dao.list(startTime, endTime);
	}

	@Autowired
	private UserInfoMng userMng;
	@Autowired
	private WiShopInfoMng shopMng;

	@Autowired
	private OrderProductMappingMng orderProductMappingMng;

	@Autowired
	private ScoreShareSetDao scoreShareSetDao;

	@Autowired
	private ScoreBuySetDao scoreBuySetDao;

	@Autowired
	private ScoreTradeDao scoreTradeDao;

	@Autowired
	private UserAccountDao userAccountDao;

	@Autowired
	private UserAccountMng userAccountMng;

	@Override
	public Pagination getPageForChildren(String school, String classes,
			String userName, String mobile, Integer productCateId,
			Integer week, String startTime, String endTime, int pageNo,
			int pageSize, CmsUser user) {
		return dao
				.getPageForChildren(school, classes, userName, mobile,
						productCateId, week, startTime, endTime, pageNo,
						pageSize, user);
	}

	@Override
	public
	List<Map<String, Object>> getPageForExcel(String school, String classes,
			String userName, String mobile, Integer productCateId,
			Integer week, String startTime, String endTime, int pageNo,
			int pageSize, CmsUser user){
		return dao.getPageForExcel(school, classes, userName, mobile, productCateId, week, startTime, endTime, pageNo, pageSize, user);
	}
}